package com.rjwh.dingdong.client.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.webkit.JavascriptInterface;

import com.rjwh.dingdong.client.activity.loginregist.ThirdPartyLoginActivity;
import com.rjwh.dingdong.client.application.MyApplication;
import com.rjwh.dingdong.client.bean.localbean.AppBaseInfo;
import com.rjwh.dingdong.client.bean.localbean.AppUserAgent;
import com.rjwh.dingdong.client.constant.LocalConstant;
import com.rjwh.dingdong.client.util.AppUtil;
import com.rjwh.dingdong.client.util.LogUtil;
import com.rjwh.dingdong.client.util.RxBus;
import com.rjwh.dingdong.client.util.ShareUtil;
import com.tencent.smtt.sdk.WebSettings;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;

/**
 * @author Kay
 * @ClassName BaseWebView
 * @Description
 * @date 2016-12-2
 * @history 2016-12-2 author: description:
 */

public class BaseWebView extends X5WebView  {
    private Observable<Integer> observable;

    public BaseWebView(Context context) {
        super(context);
        initWebSetting();
        startObserver();
    }

    public BaseWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initWebSetting();
        startObserver();
    }

    @SuppressLint({"SetJavaScriptEnabled", "AddJavascriptInterface"})
    private void initWebSetting() {
        WebSettings settings = this.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setDefaultTextEncodingName("UTF-8");  //设置默认的显示编码
        settings.setCacheMode(WebSettings.LOAD_DEFAULT);
        settings.setUseWideViewPort(false);
        settings.setSupportZoom(false);
        settings.setBuiltInZoomControls(false);
        settings.setDisplayZoomControls(false);
        settings.setBlockNetworkImage(false);
        settings.setAllowFileAccess(true);
        this.addJavascriptInterface(new MyJavascriptInterface(), "app");
    }

    public class MyJavascriptInterface {
        public MyJavascriptInterface() {
        }

        @JavascriptInterface
        public void authorize_checklogin() {
            BaseWebView.this.post(new Runnable() {
                @Override
                public void run() {
                    final boolean isLogined = MyApplication.spUtil.getBoolPreferenceByParamName(getContext(), LocalConstant.SP_IS_LOGINED);
                    if (isLogined) {
                        BaseWebView.this.loadUrl("javascript:authorize_callback_checklogin('1')", null);
                    } else {
                        BaseWebView.this.loadUrl("javascript:authorize_callback_checklogin('0')", null);
                    }
                }
            });
        }

        @JavascriptInterface
        public void authorize_fetchinfo(final String forcelogin) {
            BaseWebView.this.post(new Runnable() {
                @Override
                public void run() {
                    final boolean isLogined = MyApplication.spUtil.getBoolPreferenceByParamName(getContext(), LocalConstant.SP_IS_LOGINED);
                    if (isLogined) {
                        doCallBack();
                    } else {
                        if (forcelogin.equals("1")) {
                            ThirdPartyLoginActivity.startActivity(getContext());
                        } else if (forcelogin.equals("0")) {
                            doCallBack();
                        }
                    }
                }
            });

        }

        @JavascriptInterface
        public void share(final String title, final String content, final String url, final String image) {
            BaseWebView.this.post(new Runnable() {
                @Override
                public void run() {
                    ShareUtil.showShare(getContext(),title,content,url,image);
                }
            });
        }
    }

    private void doCallBack() {
        AppBaseInfo appBaseInfo = new AppBaseInfo(
                MyApplication.spUtil.getStrPreferenceByParamName(getContext(), LocalConstant.SP_USERID),
                MyApplication.spUtil.getStrPreferenceByParamName(getContext(), LocalConstant.SP_BABYID),
                MyApplication.spUtil.getStrPreferenceByParamName(getContext(), LocalConstant.SP_USER_TOKEN),
                "ANDROID",
                AppUtil.GetVersionName(getContext()),
                AppUtil.getSDKVersion(),
                MyApplication.deviceId
        );
        String s = appBaseInfo.toString();
        LogUtil.d("BaseWebView", s);
        BaseWebView.this.loadUrl("javascript:authorize_callback_fetchinfo('" + s + "')", null);
    }

    private void startObserver() {
        observable = RxBus.get().register(LocalConstant.RX__POST_WEB_FETCHINFO, Integer.class);
        observable.observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<Integer>() {
                               @Override
                               public void call(Integer type) {
                                   doCallBack();
                                   LogUtil.d("BaseWebView", "doCallBack()");
                               }
                           }, new Action1<Throwable>() {
                               @Override
                               public void call(Throwable throwable) {
                                   LogUtil.d("BaseWebView", throwable.getMessage());
                               }
                           }
                );
    }

    @Override
    public void destroy() {
        super.destroy();
        RxBus.get().unregister(LocalConstant.RX__POST_WEB_FETCHINFO, observable);
    }
}
